package kz.pdt12.hw.framework;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

import static org.testng.Assert.fail;

/**
 * Created with IntelliJ IDEA.
 * User: raimbek
 * Date: 07.03.13
 * Time: 0:38
 */
public class ApplicationManager {
    private WebDriver driver;
    public String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    private NavigationHelper navigationHelper;
    private GroupHelper groupHelper;
    private ContactHelper contactHelper;
    private PrintPhonesHelper printPhonesHelper;
    private Properties properties;
    private JdbcHelper jdbcHelper;
    private HibernateHelper hibernateHelper;

    private ApplicationModel model;

    public ApplicationManager(Properties properties) {
        this.properties = properties;
        model = new ApplicationModel();
        model.setGroups(getHibernateHelper().listGroups());
        model.setContacts(getHibernateHelper().listContacts());
    }

    public WebDriver getDriver() {
        if (driver == null) {
            String browser = properties.getProperty("browser");
            if (browser.equals("firefox")) {
                driver = new FirefoxDriver();
            } else if (browser.equals("ie")) {
                driver = new InternetExplorerDriver();
            } else if (browser.equals("chrome")) {
                driver = new ChromeDriver();
            } else {
                throw new Error("Unsupported browser: " + browser);
            }

            baseUrl = properties.getProperty("baseUrl");
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            driver.get(baseUrl);
        }
        return driver;
    }

    public ApplicationModel getModel() {
        return model;
    }

    public void stop() {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    public NavigationHelper navigateTo() {
        if (navigationHelper == null) {
            navigationHelper = new NavigationHelper(this);
        }
        return navigationHelper;
    }

    public GroupHelper getGroupHelper() {
        if (groupHelper == null) {
            groupHelper = new GroupHelper(this);
        }
        return groupHelper;
    }

    public ContactHelper getContactHelper() {
        if (contactHelper == null) {
            contactHelper = new ContactHelper(this);
        }
        return contactHelper;
    }

    public PrintPhonesHelper getPrintPhonesHelper() {
        if (printPhonesHelper == null) {
            printPhonesHelper = new PrintPhonesHelper(this);
        }
        return printPhonesHelper;
    }

    public JdbcHelper getJdbcHelper() {
        if (jdbcHelper == null) {
            jdbcHelper = new JdbcHelper(this, "jdbc:mysql://localhost/addressbook?user=root");
        }
        return jdbcHelper;
    }

    public HibernateHelper getHibernateHelper() {
        if (hibernateHelper == null) {
            hibernateHelper = new HibernateHelper(this);
        }
        return hibernateHelper;
    }

    public String getPropertyKey(String key) {
        return properties.getProperty(key);
    }
}